﻿@model yule.CodeBuilder.Core.PDM.TableInfo
@{
    yule.CodeBuilder.Core.IBuilder modeleBuilder = yule.CodeBuilder.Core.BuilderFactory.GetBuilder(Model, yule.CodeBuilder.Core.BuildType.Model);
    yule.CodeBuilder.Core.IBuilder baseDALBuilder = yule.CodeBuilder.Core.BuilderFactory.GetBuilder(Model, yule.CodeBuilder.Core.BuildType.BaseDAL);
    yule.CodeBuilder.Core.IBuilder iDalbuilder = yule.CodeBuilder.Core.BuilderFactory.GetBuilder(Model, yule.CodeBuilder.Core.BuildType.IDAL);
    yule.CodeBuilder.Core.IBuilder dalBuilder = yule.CodeBuilder.Core.BuilderFactory.GetBuilder(Model, yule.CodeBuilder.Core.BuildType.DAL);
}
//////////////////////////////////
///生成时间:@System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
///BaseDAL:@(string.Format("{0}({1})", Model.Name, Model.Core))
//////////////////////////////////
using System;
using System.Collections.Generic;

namespace @dalBuilder.NameSpace
{
    /// <summary>
    /// @Model.Name 数据访问类
    /// </summary>
    [yule.Common.WCF.ErrorHandlerBehavior]
    public partial class @baseDALBuilder.ClassName : yule.DAL.BaseDAL , @iDalbuilder.ClassFullName
    {
        
        /// <summary>
        /// 查询@(Model.Name)
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        public @(modeleBuilder.ClassFullName) Select(string id)
        {
            @(modeleBuilder.ClassFullName) model = this.GetCache<@(modeleBuilder.ClassFullName)>(id);

            if (model == null)
            {

                System.Text.StringBuilder sql = new System.Text.StringBuilder();

                sql.Append(string.Format("select * from {0} where {1}='{2}'", @(modeleBuilder.ClassFullName).Table.Name, @(modeleBuilder.ClassFullName).Field.Id, id));

                System.Data.DataTable dt = this.DBHelper.ExecuteDataTable(sql.ToString());

                if (dt != null && dt.Rows.Count > 0)
                {
                    model = new @(modeleBuilder.ClassFullName)();

                    model.Convert(dt.Rows[0]);

                    this.SetCache<@(modeleBuilder.ClassFullName)>(model);
                }

            }

            return model;
        }

        /// <summary>
        /// 新增@(Model.Name)
        /// </summary>
        /// <param name="model">实体</param>
        /// <param name="dbHelper"></param>
        /// <returns></returns>
        public int Insert(@(modeleBuilder.ClassFullName) model, yule.Data.IExecuteNonQuery dbHelper)
        {
            System.Text.StringBuilder sql = new System.Text.StringBuilder();

            sql.Append(string.Format(" INSERT INTO {0}", @(modeleBuilder.ClassFullName).Table.Name));
            sql.Append(" (" );
            @for (int i = 0; i < Model.Columns.Count; i++)
            {
                var column = Model.Columns[i];
                if(i>0)
                {
            @:@("sql.Append(")" ," + @(modeleBuilder.ClassFullName + ".Field." + column.Code + ");")
                }
                else
                {
            @:@("sql.Append(")" " + @(modeleBuilder.ClassFullName + ".Field." + column.Code + ");")
                }
            }
            sql.Append(" )");
            sql.Append(" VALUES");
            sql.Append(" (");
            @for (int i = 0; i < Model.Columns.Count; i++)
            {
                var column = Model.Columns[i];
                if (i > 0)
                {
            @:@("sql.Append(")" ,@@" + @(modeleBuilder.ClassFullName + ".Field." + column.Code + ");")
                }
                else
                {
            @:@("sql.Append(")" @@" + @(modeleBuilder.ClassFullName + ".Field." + column.Code + ");")
                }
            }
            sql.Append(" )");


            System.Data.Common.DbParameter[] parameters = 
            {
            @for (int i = 0; i < Model.Columns.Count; i++)
            {
                var column = Model.Columns[i];
                @:@("dbHelper.CreateDbParameter(")"@@" + @(modeleBuilder.ClassFullName + ".Field." + column.Code + ", yule.Util.Convert.To" + column.CSharpType.FirstUp() + "(model." + column.Code + ") , System.Data.DbType.AnsiString),")
            }
            };
            
            return dbHelper.ExecuteNonQuery(sql.ToString(), parameters, (cmdText, paras) =>
            {
                this.SetCache<@(modeleBuilder.ClassFullName)>(model);
            });

        }
        
        /// <summary>
        /// 删除@(Model.Name)
        /// </summary>
        /// <param name="id"></param>
        /// <param name="dbHelper"></param>
        /// <returns></returns>
        public int Delete(string id, yule.Data.IExecuteNonQuery dbHelper)
        {
            System.Text.StringBuilder sql = new System.Text.StringBuilder();

            sql.Append(string.Format("delete from {0} where {1} = '{2}'", @(modeleBuilder.ClassFullName).Table.Name, @(modeleBuilder.ClassFullName).Field.Id, id));
            
            return dbHelper.ExecuteNonQuery(sql.ToString(), (cmdText) =>
            {
                this.RemoveCache<@(modeleBuilder.ClassFullName)>(id);
            });
        }

        /// <summary>
        /// 更新@(Model.Name)
        /// </summary>
        /// <param name="model"></param>
        /// <param name="dbHelper"></param>
        /// <returns></returns>
        public int Update(@(modeleBuilder.ClassFullName) model, yule.Data.IExecuteNonQuery dbHelper)
        {
            System.Text.StringBuilder sql = new System.Text.StringBuilder();
            sql.Append(string.Format("update {0}", yule.Model.Sys.Region.Table.Name));
            sql.Append(" SET");
            @for (int i = 0; i < Model.Columns.Count; i++)
            {
                var column = Model.Columns[i];
                if(column.Name!="Id"){
                    if(i>0)
                    {
            @:@("sql.Append(")" ," + @(modeleBuilder.ClassFullName + ".Field." + column.Code) + "=@@" + @(modeleBuilder.ClassFullName + ".Field." + column.Code+");")
                    }
                    else
                    {
            @:@("sql.Append(")" " + @(modeleBuilder.ClassFullName + ".Field." + column.Code)  + "=@@" + @(modeleBuilder.ClassFullName + ".Field." + column.Code + ");")
                    }
                }
            }
            @("sql.Append(\" WHERE\");")
            @("sql.Append(")" " + @(modeleBuilder.ClassFullName+".Field.Id") + "=@@" + @(modeleBuilder.ClassFullName+".Field.Id);");

            System.Data.Common.DbParameter[] parameters = 
            {
            @for (int i = 0; i < Model.Columns.Count; i++)
            {
                var column = Model.Columns[i];
                @:@("dbHelper.CreateDbParameter(")"@@" + @(modeleBuilder.ClassFullName + ".Field." + column.Code + ", yule.Util.Convert.To" + column.CSharpType.FirstUp() + "(model." + column.Code + ") , System.Data.DbType.AnsiString),")
            }
            };


            return dbHelper.ExecuteNonQuery(sql.ToString(), parameters, (cmdText, paras) =>
            {
                this.RemoveCache<@(modeleBuilder.ClassFullName)>(model);
            });
        }
        
        /// <summary>
        /// 查询所有@(Model.Name)
        /// </summary>
        /// <returns></returns>
        public IList<@(modeleBuilder.ClassFullName)> List()
        {
            yule.Data.IExecuteQuery dbHelper = yule.Data.DBHelper.Instance;

            System.Text.StringBuilder sql = new System.Text.StringBuilder();

            sql.Append("select * from " + @(modeleBuilder.ClassFullName).Table.Name);

            System.Data.DataTable dt = dbHelper.ExecuteDataTable(sql.ToString());

            IList<@(modeleBuilder.ClassFullName)> list = this.ConverToList<@(modeleBuilder.ClassFullName)>(dt,
                (model) => { this.SetCache<@(modeleBuilder.ClassFullName)>(model); });

            return list;
        }

    }
}